/* -----------------------------------------------------------------------
 * random test council_room
 * -----------------------------------------------------------------------
 */

#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"
#include<time.h>
#include <stdlib.h>
// set NOISY_TEST to 0 to remove printfs from output
#define NOISY_TEST 1

int main() {
	// based off of testUpdateCoins  example provided for class
    int i;
    int seed = 1000;
    int numPlayer = 2;

	int r;
    int k[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, baron, great_hall};
    struct gameState G;

	int choice1 = 0;
	int choice2 = 0;
	int choice3 = 0;
	int handPos = 0;
	int bonus;

	int player = 0;

	int j =0;
	
	int random;
	int deck_or_discard = 2;
	int position;
	int treasure[] = {copper, silver, gold, estate, estate, estate, estate, estate};
	int treasureSize = sizeof(treasure)/sizeof(treasure[0]);
	int test;
	int size = 20;
	int full;
	int treasureCount = 0;
	int pass = 1;
	int loops = 10;
	int full_1;
	int n;
	
	srand(time(NULL));
	
	memset(&G, 23, sizeof(struct gameState));   // clear the game state
	r = initializeGame(numPlayer, k, seed, &G); // initialize a new game
	
	//==============================================================================================================
	
	//repeat tests
	for(j=0; j<loops; j++){
		
		//for each player set initial card counts for deck
		for(i=0; i<numPlayer; i++){	
			
			player = i;
			G.deckCount[player] = 0;
			G.handCount[player] = 1; //start with one card to account for card being played
			G.discardCount[player] = 0;

			//set a random placment of cards in discard or deck
			random = rand()% deck_or_discard;
			full = rand() % size +1;
			
			//store deck amount
			if(player == 0){
				full_1 = full;}
			
			//randomly fill deck from array of selected cards
			if(random == 0){
				for(n=0; n<full; n++){
					position = G.deckCount[player];
					G.deck[player][position] = treasure[rand()%treasureSize];
					G.deckCount[player] += 1;
				}
			}
			else{
				for(n=0; n<full; n++){
					position = G.discardCount[player];
					G.discard[player][position] = treasure[rand()%treasureSize];
					G.discardCount[player] += 1;
				}
			}//end else
		}//end for
		//==============================================================================================================

		//play card
		test = cardEffect(council_room, choice1, choice2, choice3, &G, handPos, &bonus);
		if(test == -1){
			printf("RandomTestCard FAILED, card effect return -1!\n");
		}
		
		
		full = full_1;	
		player = 0;
		
		//player hand count should have added four cards unless number of card was unavailbe
		if(full < 4 && G.handCount[player] != full){
			pass = 0; 
			printf("**1FAILED** RandomTestCard: -- Loop = %i, wrong handCount1 = %i, should be = %i\n",	j, G.handCount[player], full);
		}
		//check if hand count is correct
		else if(full >= 4 && G.handCount[player] != 4){
			printf("**2FAILED** RandomTestCard: -- Loop = %i, wrong handCount2 = %i, should be = %i\n",	j, G.handCount[player], 4);
		}
		
		//check if num buys increased
		if(G.numBuys < 2){
			pass = 0;
			printf("**3FAILED** RandomTestCard: -- Loop = %i, wrong numBuys = %i, should be = 2\n",	j, G.numBuys);
		}		
		
		//check that oppenent drew card
		if(G.handCount[player+1] != 2){
			pass = 0; 
			printf("**4FAILED** RandomTestCard: -- Loop = %i, wrong opponent handCount = %i, should be = %i\n",	j, G.handCount[player+1], 2);
		}	
		
	}//end test loop
	
	if(pass == 1){
		printf("RandomTestCard: All tests Passed\n");
	}
	
	
    return 0;
}